<?php

namespace App\Http\Resources\Api;

use App\Models\GroupCourse;
use App\Models\Tenant;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;

class GroupCourseResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $tenant = Tenant::find($this->tenant_id);
        $cover_url = config('app.url') . "uploads/{$tenant->admin_prefix}/" . $this->cover_url;
        $coach_cover_url = config('app.url') . "uploads/{$tenant->admin_prefix}/" . $this->coach->cover_url;
        $course_time = date('H:i', strtotime($this->course_start_time)) . ' ~ ' . date('H:i', strtotime($this->course_end_time));
        if ($this->schedule_type == GroupCourse::SCHEDULE_TYPE_DATE) {
            $full_course_time = $this->date . ' ' . GroupCourse::WEEKS[$this->week] . ' ' . $course_time;
            $date = Carbon::now()->weekday($this->week - 1)->toDateString();
            $start_time = $date . ' ' . $this->course_start_time;
        } else {
            $date = Carbon::now()->weekday($this->week - 1)->toDateString();
            $full_course_time = $date . ' ' . GroupCourse::WEEKS[$this->week] . ' ' . $course_time;
            $start_time = $this->date . ' ' . $this->course_start_time;
        }
        $end_order_time = $this->deadline ? Carbon::parse($start_time)->subMinutes($this->deadline)->toDateTimeString() : '不限';
        return [
            'id'=>$this->id,
            'title' => $this->title,
            'cover_url' => $cover_url,
            'coach_cover_url' => $coach_cover_url,
            'coach_name' => $this->coach->realname,
            'coach_brief_intro' => $this->coach->brief_intro,
            'price' => $this->price > 0 ? "¥{$this->price}" : '免费',
            'tags' => $this->tags,
            'tags_str' => implode('、', $this->tags),
            'people_number' => $this->people_number,
            'schedule_type' => $this->schedule_type,
            'week' => $this->week,
            'week_name' => GroupCourse::WEEKS[$this->week],
            'date' => $this->date,
            'course_time' => $course_time,
            'full_course_time' => $full_course_time,
            'deadline' => $this->deadline,
            'end_order_time' => $end_order_time,
            'intro' => Str::limit($this->intro, 40, '...'),
            'notes' => Str::limit($this->notes, 28, '...'),
            'shop_name' => $this->tenant->short_name ? $this->tenant->short_name : $this->tenant->name,
            'shop_address' => $this->tenant->address,
            'shop_latitude' => $this->tenant->latitude,
            'shop_longitude' => $this->tenant->longitude,
            'order_users' => [],
            'need_pay' => $this->price > 0 ? true : false,
            'created_at'=>(string)$this->created_at,
            'updated_at'=>(string)$this->updated_at
        ];
    }
}
